<?php
defined ( 'IN_TS' ) or die ( 'Access Denied.' );

// 用户是否登录
$userid = aac ( 'user' )->isLogin ();

switch ($ts) {
	
	case "do" :


		$articleid = intval ( $_POST ['articleid'] );
		$content = tsClean ( $_POST ['content'] );

		$text = strip_tags($_POST['content'],'<img>');


		if($text == ''){
			tsNotice('评论内容不能为空');
		}



			// 过滤内容开始
		aac ( 'system' )->antiWord ( $content );
		// 过滤内容结束

		$new ['article']->create ( 'article_comment', array (

				'articleid' => $articleid,
				'userid' => $userid,
				'content' => $content,
				'addtime' => time ()
		)
		 );



		// 统计
		$count_comment = $new ['article']->findCount ( 'article_comment', array (
				'articleid' => $articleid
		) );

		$new ['article']->update ( 'article', array (
				'articleid' => $articleid
		), array (
				'count_comment' => $count_comment
		) );

		//发送系统消息(通知楼主有人回复他的话题啦)
		$strArticle = $new ['article']->find('article',array(
			'articleid'=>$articleid,
		));

		if($strArticle['userid'] != $TS_USER['userid']){

			$msg_userid = '0';
			$msg_touserid = $strArticle['userid'];
			$msg_content = '你的文档：《'.$strArticle['title'].'》新增一条评论，快去看看吧^_^ ';
			$msg_tourl = tsUrl('article','show',array('id'=>$articleid));
			aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);

		}

		$content = '评论了文档《<a href="'.tsUrl('article','show',array('id'=>$articleid)).'">'.$strArticle['title'].'》！</a>';
		aac('weibo')->create('weibo',array(
			'userid'=>$TS_USER['userid'],
			'content'=>$content,
			'isaudit'=>0,
			'addtime'=>date('Y-m-d H:i:s'),
			'uptime'=>date('Y-m-d H:i:s'),
		));


		header ( "Location: " . tsUrl ( 'article', 'show', array (
				'id' => $articleid
			) ) );

		break;
	
	case "delete" :
		
		$commentid = intval ( $_GET ['commentid'] );
		
		$strComment = $new ['article']->find ( 'article_comment', array (
				'commentid' => $commentid 
		) );
		
		$strArticle = $new ['article']->find ( 'article', array (
				'articleid' => $strComment ['articleid'] 
		) );
		
		if ($userid == $strComment ['userid'] || $TS_USER ['isadmin'] == 1) {
			
			$new ['article']->delete ( 'article_comment', array (
					
					'commentid' => $commentid 
			)
			 );

			header ( "Location: " . tsUrl ( 'article', 'show', array (
					'id' =>  $strComment ['articleid']
				) ) );

			// 统计
			$count_comment = $new ['article']->findCount ( 'article_comment', array (
				'articleid' =>  $strComment ['articleid']
			) );

			$new ['article']->update ( 'article', array (
				'articleid' =>  $strComment ['articleid']
			), array (
				'count_comment' => $count_comment
			) );

			
			tsNotice ( '删除成功' );
		} else {
			
			tsNotice ( '非法操作' );
		}
		
		break;

	//回复评论
	case "recomment":


		$referid = intval($_POST['referid']);
		$articleid = intval($_POST['articleid']);
		$content = tsClean($_POST['content']);

		if ($content == ''){
			tsNotice ( "内容不能为空！" );
		}


		// 过滤内容开始
		aac ( 'system' )->antiWord ( $content );
		// 过滤内容结束
		//

		$new['article']->create('article_comment',array(
			'referid'=>$referid,
			'articleid'=>$articleid,
			'userid'=>$userid,
			'content'=>$content,
			'addtime'=>time(),
		));

		header ( "Location: " . tsUrl ( 'article', 'show', array (
				'id' => $articleid
			) ) );
		//统计评论数
		$count_comment = $new['article']->findCount('article_comment',array(
			'articleid'=>$articleid,
		));

		//更新话题最后回应时间和评论数
		$new['article']->update('article',array(
			'articleid'=>$articleid,
		),array(
			'count_comment'=>$count_comment,
			'uptime'=>time(),
		));

		$strArticle = $new['article']->find('article',array(
			'articleid'=>$articleid,
		));

		$strComment = $new['article']->find('article_comment',array(
			'commentid'=>$referid,
		));

		if($articleid && $strArticle['userid'] != $TS_USER['userid']){
			$msg_userid = '0';
			$msg_touserid = $strArticle['userid'];
			$msg_content = '你的文档：《'.$strArticle['title'].'》新增一条评论，快去看看给个回复吧^_^ ';
			$msg_tourl = tsUrl('article','show',array('id'=>$articleid));
			aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
		}

		if($referid && $strComment['userid'] != $TS_USER['userid']){
			$msg_userid = '0';
			$msg_touserid = $strComment['userid'];
			$msg_content = '有人回复了你在文档《'.$strArticle['title'].'》中的评论，快去看看给个回复吧^_^ ';
			$msg_tourl = tsUrl('article','show',array('id'=>$articleid));
			aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
		}

		$content = '评论了文档《<a href="'.tsUrl('article','show',array('id'=>$articleid)).'">'.$strArticle['title'].'》！</a>';
		aac('weibo')->create('weibo',array(
			'userid'=>$TS_USER['userid'],
			'content'=>$content,
			'isaudit'=>0,
			'addtime'=>date('Y-m-d H:i:s'),
			'uptime'=>date('Y-m-d H:i:s'),
		));

		header ( "Location: " . tsUrl ( 'article', 'show', array (
				'id' => $articleid
			) ) );

		break;
}